package com.situ.stl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.stl.dao.CourseMapper;
import com.situ.stl.dao.ReserveMapper;
import com.situ.stl.entity.Course;
import com.situ.stl.entity.Reserve;
import com.situ.stl.service.CourseService;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/** 
 * Service层 - 课程
 */
@Service 
public class CourseServiceImpl implements CourseService{

    /** 
     */
    @Autowired 
    private CourseMapper courseMapper;
    @Autowired
    private ReserveMapper reserveMapper;

    /** 
     * add
     */
    @Override 
    public int add( Course course) {
        int res = courseMapper.insert(course);
        return res;
    }

    /** 
     * remove
     */
    @Override 
    public int remove( int id) throws Exception {
        Course sCourse = courseMapper.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(sCourse)) {
            throw new Exception("课程不存在！");
        }

        List<Reserve> reserves = reserveMapper.selectByCourse(id);
        if (!reserves.isEmpty()){
            throw new Exception("课程存在预约，无法删除！");
        }

        int res = courseMapper.delete(id);
        return res;
    }

    /** 
     * edit
     */
    @Override 
    public int edit( Course course) {
        int res = courseMapper.update(course);
        return res;
    }

    /** 
     * getById
     */
    @Override 
    public Course getById( int id) {
        return courseMapper.selectByPrimaryKey(id);
    }

    /** 
     * get All
     */
    @Override 
    public List<Course> getAll() {
        return courseMapper.selectAll();
    }

    /** 
     * search
     */
    @Override 
    public List search( Course course) {
        return courseMapper.select(course);
    }

    /** 
     * searchByPage
     */
    @Override 
    public PageInfo searchByPage( Integer page, Integer limit, Course course) {
        if (page == null) page = 1;
        if (limit == null) limit = 10;
        
        PageHelper.startPage(page, limit);
        List list = courseMapper.select(course);
        PageInfo pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

}